'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "应收款新增";
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            $scope.record={};
            $scope.data=[];
            selBank();
            //selAdvertsList();
            $scope.record.receiptType ="3";
            //初始化验证
            $scope.osubmit = function(status){
                $scope.loading = true
                $scope.record.tradeDate=$('#tradeDate').val();
                $scope.record.companyName=$('#companyName').val();
                $scope.record.status=status;
                if($scope.record.amount<=0){
                    $scope.alertModal({'content':'充值金额不得小于0'});
                    return;
                }
                $('#form').submit();
                    }
            // 查询正常的广告主
            function selAdvertsList() {
                $scope.loading = true;
                $.ajax({
                    url: $scope.app.project_name + '/userAdverts/read/all',
                    data: {}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.advertsList = result.data;
                       /* $scope.record.userIdS = $scope.advertsList[0].id;*/
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            //查询银行
            function selBank(){
                $scope.loading = true;
                $.ajax({
                    url: $scope.app.project_name + '/accountRec/read/bankList',
                    data: {}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.bankList = result.data;
                        $scope.record.payeeBank = $scope.bankList[0].bankCode;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });

            }
           $('#form').validate({
                rules: {
                    amount: {
                        required: true
                    },
                    payeeAccount: {
                        required: true
                    },
                    payeeBank: {
                        required: true
                    },
                    info: {
                        required: true
                    },
                    bankTradeNo: {
                        required: true
                    },
                    tradeDate: {
                        required: true
                    }



                },
                messages: {
                    amount: {
                        required: '请输入充值金额'
                    },
                    payeeAccount: {
                        required: '请输入付款账户'
                    },
                    payeeBank: {
                        required: '请输入行别'
                    },
                    info: {
                        required: '请输入汇款单位/个人'
                    },
                    bankTradeNo: {
                        required: '请输入交易流水号'
                    },
                    tradeDate: {
                        required: '请输入交易日期'
                    }
                },
                submitHandler: function() {
                    var m = $scope.record;
                   $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url:$scope.app.project_name + '/accountRec/add/addRecharge',
                        data: m
                    }).then(function (result) {
                        if (result && result.httpCode == 200) {//成功
                            if(result.data=='-1'){
                                $scope.alertModal({"content":"没有该广告主，请确认后再次添加"});
                                $scope.isDisabled = false;
                            }else if(result.data=='-2'){
                                $scope.alertModal({"content":"该广告主没有子账户信息，请确认后再次添加"});
                                $scope.isDisabled = false;
                            } else{
                            $scope.alertModal({"content":"申请成功"});
                            if($scope.record.status==1){
                                $scope.record={};
                                $scope.record.receiptType ="3";
                                $scope.record.payeeBank = $scope.bankList[0].bankCode;
                               /* $scope.record.userIdS = $scope.advertsList[0].id;*/
                                $scope.isDisabled = false;//提交disabled
                                $('#tradeDate').val("");
                                $state.go('main.finance.accountRec.create');
                                $scope.$apply();
                            }else{
                                $scope.record={};
                                $state.go('main.finance.accountRec.list');
                                $scope.$apply();
                            }
                        }
                        }else {
                            $scope.alertModal({"content":"申请失败，请稍后重试!"});
                            $state.go('main.finance.accountRec.list');
                            $scope.$apply();
                        }
                        $scope.$apply();
                    });
                }
            });
            $scope.itemSelectFunction = function(id,companyName) {
                $("#companyName").val(companyName);
            };
             $scope.showTheList=function()
            {
                var adverName=$('#companyName').val();
                $.ajax({
                    url:$scope.app.project_name + '/userAdverts/read/all',
                    data : {
                        adverName : adverName
                    },
                }).then(function (result) {
                    if (result && result.httpCode == 200) {//成功
                        $scope.data = result.data;
                        $scope.$apply();
                    }
                });
            }
          $scope.removeTheList=function() {
               $timeout(function () {
                   $scope.data=[];
               },1000)
            }
           /* $scope.inListColorAdd=function(id, width){
                obj.className="inListMove";
                $('inListOut'+id).width(width);
            }

            $scope.inListColorMove=function(id, width){
                obj.className="inListOut";
                $('inListOut'+id).width(width);
            }*/

        }]);

